package Game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

public class Window extends JFrame {
    int w=1100,h=910;
    Head head=new Head(Resource.head_r,14+30*1,877-30*1,this);//蛇头对象

    public List<Body> bodyList=new ArrayList<>();//蛇身集合

    Food food1=new Food().getFood();
    Food food2=new Food().getFood();
    Food food3=new Food().getFood();

    public static int score=0;

    public static int state=0;//游戏状态: 0.未开始 1.游戏中 2.暂停  3.失败  4.通关  5.重启  6.下一关

    Image Cacheimage=null;
    Graphics cimage;

    public void Launch() throws InterruptedException {//窗口创建
        this.setVisible(true);//窗口是否可见
        this.setSize(w, h);//窗口大小
        this.setLocationRelativeTo(null);//窗口位置居中
        this.setTitle("贪吃蛇");

        bodyList.add(new Body(Resource.body,30,878,this));//初始蛇身1
        bodyList.add(new Body(Resource.body,0,878,this));//初始蛇身2

        this.addKeyListener(new KeyAdapter(){
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_SPACE){
                    switch(state){
                        case 0://未开始
                            state=1;
                            break;
                        case 1://游戏中
                            state=2;
                            repaint();
                            break;
                        case 2://暂停
                            state=1;
                            break;
                        case 3://失败后重启
                            state=5;
                            break;
                        case 4:
                            state=6;
                        default:
                            break;
                    }
                }
            }
        });

        while(true){//刷新
            if(Cacheimage==null){
                Cacheimage=this.createImage(w,h);
            }
            cimage=Cacheimage.getGraphics();
            if(state==1){
                repaint();
            }
            if(state==5){
                state=0;
                regame();
            }
            if(state==6 && Resource.level!=5){
                state=1;
                score=0;
                Resource.level++;
                regame();
            }
            Thread.sleep(200);
        }
    }



    public void paint(Graphics g) {//绘制

        cimage.setColor(Color.white);//白色背景
        cimage.fillRect(0, 0, w, h);//填充画面

        //网格绘制
        cimage.setColor(Color.black);//黑线
        for (int i = 0; i <=30; i++) {//绘制20x20的网格
            cimage.drawLine(0, i*30, 908, i*30);//绘制横线
            cimage.drawLine(i*30+8, 0, i*30+8, 900);//绘制竖线
        }


        for(int x=bodyList.size()-1;x>=0;x--){//蛇身绘制
            bodyList.get(x).paintSelf(cimage);
        }

        head.paintSelf(cimage);//蛇头绘制

        food1.paintSelf(cimage);
        if(Resource.level>=4){
            food2.paintSelf(cimage);
            food3.paintSelf(cimage);
        }

        Resource.Word(cimage,"第"+Resource.level+"关",Color.orange,40,950,400);

        Resource.Word(cimage,score+"分",Color.red,50,968,455);

        cimage.setColor(Color.black);
        prompt(cimage);

        g.drawImage(Cacheimage,0,0,null);
    }

    void prompt(Graphics g){//提示
        if(state==0){//未开始
            g.fillRect(258,399,400,70);
            Resource.Word(g,"按下空格开始",Color.white,35,351,446);
        }
        if(state==2){//暂停
            g.fillRect(258,399,400,70);
            Resource.Word(g,"游戏暂停",Color.white,35,384,446);
        }
        if(state==4){//通关
            g.fillRect(258,399,400,70);
            Resource.Word(g,"游戏通关",Color.white,35,386,446);
        }
        if(state==3){//失败
            g.fillRect(258,399,400,70);
            Resource.Word(g,"游戏失败",Color.white,35,384,446);
        }
    }

    void regame() throws InterruptedException {
        this.dispose();
        String[] args= {"114514"};
        main(args);
    }

    public static void main(String[] args) throws InterruptedException {
        Window window = new Window();
        window.Launch();
    }
}
